package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.Pet;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 宠物表 Mapper 接口
 * </p>
 *
 * @author Mr.we
 * @since 2024-06-04
 */
@Mapper
public interface PetMapper extends BaseMapper<Pet> {
    //查询所有数据
    @Select("SELECT * FROM pet")
    List<Pet> findAll();

    //添加宠物
    @Insert("INSERT INTO pet(pet_name,pet_sex,pet_age,pet_img,pet_info,create_time) VALUES(#{petName},#{petSex},#{petAge},#{petImg},#{petInfo},NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "petId")
    int addPet(Pet pet);

    //通过宠物id查询宠物
    @Select("SELECT * FROM pet WHERE pet_id = #{petId}")
    Pet findPetById(@Param("petId") Integer petId);

    //多条件查询宠物


    //根据用户id查询对应的宠物
    @Select("SELECT t3.user_id,t3.nickname,t1.pet_name,t1.pet_img,t1.pet_sex,t1.pet_info FROM pet t1\n" +
            "INNER JOIN pet_and_user t2\n" +
            "ON t1.pet_id = t2.pet_id\n" +
            "INNER JOIN user t3\n" +
            "ON t3.user_id = #{userId}\n")
    List<Pet> findPetByUserId(@Param("userId") Integer userId);

    //根据宠物id删除对应的宠物
    @Delete("DELETE FROM pet WHERE pet_id = #{petId}")
    int deletePetById(@Param("petId") Integer petId);
}
